package com.ldc.wandroid.ui.fragments;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.ldc.common.mvvm.BaseMvvmFragment;
import com.ldc.common.utils.ViewModelUtils;
import com.ldc.wandroid.adapter.SystemAdapter;
import com.ldc.wandroid.databinding.FragmentSystemBinding;
import com.ldc.wandroid.model.SystemModel;
import com.ldc.wandroid.ui.activitys.SystemInfoActivity;
import com.ldc.wandroid.viewmodel.SystemViewModel;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class SystemFragment extends BaseMvvmFragment<FragmentSystemBinding, SystemViewModel> {

    private SystemAdapter systemAdapter;

    public static SystemFragment newInstance() {
        Bundle args = new Bundle();
        SystemFragment fragment = new SystemFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        viewModel.getSystemData().removeObserver(this::systemData);
    }

    @Override
    protected FragmentSystemBinding initViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return FragmentSystemBinding.inflate(inflater, container, false);
    }

    @Override
    protected void main(Bundle savedInstanceState) {
        viewBinding.stateView.showLoadingView();
        initAdapter();
        // 观察数据变化
        viewModel.getSystemData().observeForever(this::systemData);
        // 记载数据
        viewModel.loadSystem();
    }

    @Override
    protected SystemViewModel initViewModel() {
        return ViewModelUtils.getViewModel(this, SystemViewModel.class);
    }

    private void systemData(List<SystemModel> models) {
        viewBinding.contentView.refreshView.finishRefresh();
        viewBinding.stateView.showContentView();
        if (null == models) {
            return;
        }
        systemAdapter.setNewData(models);
    }

    private void initAdapter() {
        viewBinding.contentView.refreshView.setEnableRefresh(true);
        viewBinding.contentView.refreshView.setEnableLoadMore(false);
        viewBinding.contentView.refreshView.setOnRefreshListener(refreshLayout -> {
            viewBinding.stateView.showLoadingView();
            viewModel.loadSystem();
        });
        if (systemAdapter == null) {
            systemAdapter = new SystemAdapter();
        }
        systemAdapter.setOnLabelClickListener((label, data, position) -> {
            SystemModel.ChildrenBean model = (SystemModel.ChildrenBean) data;
            SystemInfoActivity.actionStart(getActivity(), model.getName(), String.format("%s", model.getId()));
        });
        viewBinding.contentView.recyclerView.setAdapter(systemAdapter);
        viewBinding.contentView.recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        viewBinding.contentView.recyclerView.setHasFixedSize(true);
    }
}
